package com.uit.gis.pojo;

import com.uit.gis.util.JsonData;
import com.uit.gis.util.Util.SpatialType;

import processing.data.JSONObject;

public class Ellipse extends Shape {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JSONObject ellipseObj = new JSONObject();

	int ellipseX, ellipseY, ellipseWidth, ellipseHeight, clickCount;

	@Override
	public void setup() {
		//size(800, 600);
		background(255);
		initialize();
		spatialType = SpatialType.CUSTOMSHAPE;
	}

 
	void initialize() {
		clickCount = 0;
		ellipseX = -1;
		ellipseY = -1;
		ellipseWidth = -1;
		ellipseHeight = -1;
	}

	public void draw() {
		if (!continueDraw)
			return;
		if (ellipseX > 0 && ellipseY > 0 && clickCount != 0) {
			background(255);
			stroke(0);
			//fill(175);
			ellipseWidth = mouseX - ellipseX;
			ellipseHeight = mouseY - ellipseY;
			ellipse(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
		}
	}

	public void mousePressed() {
		if (!continueDraw)
			return;
		if (clickCount == 0) {
			ellipseX = mouseX;
			ellipseY = mouseY;
			clickCount++;
		} else {
//			ellipseObj.setInt("x", ellipseX);
//			ellipseObj.setInt("y", ellipseY);
//			ellipseObj.setInt("w", ellipseWidth);
//			ellipseObj.setInt("h", ellipseHeight);
//			initialize();
		}
	}

	@Override
	public void mouseClicked(processing.event.MouseEvent e) {
		if (e.getButton() == processing.core.PConstants.RIGHT) {
			//saveJSONObject(ellipseObj, "ellipse.json");
			ellipseObj.setInt("x", ellipseX);
			ellipseObj.setInt("y", ellipseY);
			ellipseObj.setInt("w", ellipseWidth);
			ellipseObj.setInt("h", ellipseHeight);
			saveToTextFile();
			continueDraw = false;
		}
		else if(e.getButton() == processing.core.PConstants.LEFT && !continueDraw)
		{
//			ellipseObj.setInt("x", ellipseX);
//			ellipseObj.setInt("y", ellipseY);
//			ellipseObj.setInt("w", ellipseWidth);
//			ellipseObj.setInt("h", ellipseHeight);
			initialize();
			continueDraw = true; //resume
		}
	}

	@Override
	public void saveToTextFile() {
		// TODO Auto-generated method stub
		saveJSONObject(ellipseObj, getSavingPath());
		super.saveToTextFile();
	};
	@Override
	public JsonData loadDataFromFile() {
		// TODO Auto-generated method stub
		JSONObject jsonObject = loadJSONObject(getSavingPath());
		JsonData jsonData = new JsonData(null, jsonObject);
		return jsonData;
	};
	@Override
	public String getSpatialType() {
		// TODO Auto-generated method stub
		return SpatialType.ELLIPSE.type;
	};

}
